﻿Imports System.Collections.Generic

Namespace DoFactory.GangOfFour.Strategy.RealWorld


    ''' <summary>
    ''' MainApp startup class for Real-World 
    ''' Strategy Design Pattern.
    ''' </summary>
    Public Class MainApp

        ''' <summary>
        ''' Entry point into console application.
        ''' </summary>
        Public Shared Sub Main()
            ' Two contexts following different strategies
            Dim studentRecords As New SortedList()

            studentRecords.Add("Samual")

            studentRecords.Add("Jimmy")

            studentRecords.Add("Sandra")

            studentRecords.Add("Vivek")

            studentRecords.Add("Anna")

            studentRecords.SetSortStrategy(New QuickSort())

            studentRecords.Sort()

            studentRecords.SetSortStrategy(New ShellSort())

            studentRecords.Sort()

            studentRecords.SetSortStrategy(New MergeSort())

            studentRecords.Sort()

            ' Wait for user
            'Console.ReadKey()

        End Sub

    End Class



    ''' <summary>
    ''' The 'Strategy' abstract class
    ''' </summary>
    MustInherit Class SortStrategy

        Public MustOverride Sub Sort(ByVal list As List(Of String))

    End Class

    ''' <summary>
    ''' A 'ConcreteStrategy' class
    ''' </summary>
    Class QuickSort
        Inherits SortStrategy

        Public Overrides Sub Sort(ByVal list As List(Of String))
            list.Sort()
            ' Default is Quicksort
            Console.WriteLine("QuickSorted list ")
        End Sub

    End Class


    ''' <summary>
    ''' A 'ConcreteStrategy' class
    ''' </summary>
    Class ShellSort
        Inherits SortStrategy

        Public Overrides Sub Sort(ByVal list As List(Of String))
            'list.ShellSort(); not-implemented
            Console.WriteLine("ShellSorted list ")
        End Sub

    End Class

    ''' <summary>
    ''' A 'ConcreteStrategy' class
    ''' </summary>
    Class MergeSort
        Inherits SortStrategy

        Public Overrides Sub Sort(ByVal list As List(Of String))
            'list.MergeSort(); not-implemented
            Console.WriteLine("MergeSorted list ")
        End Sub

    End Class


    ''' <summary>
    ''' The 'Context' class
    ''' </summary>
    Class SortedList

        Private _list As New List(Of String)()

        Private _sortstrategy As SortStrategy

        Public Sub SetSortStrategy(ByVal sortstrategy As SortStrategy)
            Me._sortstrategy = sortstrategy
        End Sub

        Public Sub Add(ByVal name As String)
            _list.Add(name)
        End Sub

        Public Sub Sort()
            _sortstrategy.Sort(_list)
            ' Iterate over list and display results
            For Each name As String In _list
                Console.WriteLine(" " & name)
            Next

            Console.WriteLine()

        End Sub

    End Class

End Namespace
